package pharao.client.views.game.registrierung;

import hmvc4gwt.client.event.IHmvcEvent;
import hmvc4gwt.framework.client.events.TerminateEvent;
import hmvc4gwt.framework.client.view.GuiView;
import pharao.client.dto.UserDTO;
import pharao.client.i18ln.GuiText;
import pharao.client.views.game.login.events.SetLoginUserEvent;
import pharao.client.views.navigation.events.AddGameNavigationEvent;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class RegistrierungsView extends GuiView {
	
	/**
	 * enthält das Username-Widget
	 */
	private TextBox textBoxUsername = new TextBox();
	
	/**
	 * enthält das Password-Widget
	 */
	private PasswordTextBox textboxPassword = new PasswordTextBox();
	
	/**
	 * enthält den LoginButton
	 */
	private Button buttonLogin = new Button();
	
	/**
	 * enthält die aktuelle flexTable
	 */
	private FlexTable currentFlexTable;

	public void initImpl() {
		
		/*
		 * Text anzeigen
		 */
		Label label = new Label(GuiText.constants.text_registrierung());
		add(label);
		
		/*
		 * ClickListener setzen
		 */
		buttonLogin.setText(GuiText.constants.button_login());
		buttonLogin.setStylePrimaryName("buttonLogin");
		
		buttonLogin.addClickListener(new ClickListener() {
			
			public void onClick(Widget sender) {
				if (sender == buttonLogin) {
					registerAction();
				}
			}
		});
		
		//TODO password Länge begrenzen
		textBoxUsername.setMaxLength(12);
		
		initView();
	}

	public void handleEventImpl(IHmvcEvent event) {
		// TODO Auto-generated method stub

	}

	private void initView() {		
		
		buttonLogin.setEnabled(false);
		textboxPassword.setText(null);
		
		FlexTable flexTable = new FlexTable();
		flexTable.setStylePrimaryName("tableLogin");
		
		flexTable.setText(0, 0, GuiText.constants.link_login_username());
		flexTable.setText(1, 0, GuiText.constants.link_login_password());
		
		flexTable.setWidget(0, 1, textBoxUsername);
		flexTable.setWidget(1, 1, textboxPassword);
		
		flexTable.setWidget(2, 1, buttonLogin);
		
		/*
		 * Anzeigen löschen
		 */
		if (currentFlexTable != null) {
			remove(currentFlexTable);
		}
		
		currentFlexTable = flexTable;
		
		/*
		 * neue FlexTable anzeigen
		 */
		add(flexTable);
		
		/*
		 * Button aktivieren
		 */
		buttonLogin.setEnabled(true);
	}
	
	private void registerAction() {
		
		//TODO auf empty passwort prüfen
		if ((textBoxUsername.getText().equals(""))) {			
			
			String msg = null;
			
			if (textBoxUsername.getText().equals("")) {
				msg = GuiText.constants.window_alert_game_register_usernameEmpty();
			}
			
			if (textboxPassword.getText().equals("")) {
				if (msg == null) {
					msg = GuiText.constants.window_alert_game_register_passwordEmpty();
				} else {
					msg = msg + " " + GuiText.constants.window_alert_game_register_passwordEmpty();
				}
			}
			
			Window.alert(msg);
			
			/*
			 * Passwort zurücksetzen
			 */
			textboxPassword.setText("");
			
			return;
			
		}
		
		/*
		 * User erstellen
		 * TODO user in der Datenbank anlegen
		 */		
		UserDTO userDTO = UserDTO.getTestUserDTO(textBoxUsername.getText());
		
		/*
		 * Eingeloggten User setzen
		 */
		getController().fireEvent(new SetLoginUserEvent(userDTO), true, false, false);
		
		/*
		 * GameNavigationController hinzufügen
		 */
		getController().fireEvent(new AddGameNavigationEvent(), true, false, false);
		
		/*
		 * Registrierung entfernen
		 */
		getController().fireEventLocal(new TerminateEvent());
	}

	public void terminate() {
		// TODO Auto-generated method stub
		
	}
}
